<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <h1>get请求</h1>
  <input type="text" id="ipt">
  <button id="btn">点击提交请求</button>
  <script>
    const obtn = document.getElementById('btn')
    const oipt = document.getElementById('ipt')

    obtn.onclick = function () {
      const userId = oipt.value
      // 创建ajax实例
      const xhr = new XMLHttpRequest()
      // 打开路径
      // xhr.open('GET', `/user/${userId}?_=${Date.now()}`, true)
      xhr.open('GET', `/user?userId=${userId}&_=${Date.now()}`, true)
      // 发送
      xhr.send()
      // 绑定事件
      xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status <= 299)) {
          console.log(JSON.parse(xhr.responseText));
        }
      }
    }
  </script>
</body>

</html>